package com.chahu.filter;

import com.chahu.entity.User;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")
public class LoginFilter extends HttpFilter {

    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
        String uri = req.getRequestURI();
        if(!uri.contains("/static") && !uri.contains("login") && !uri.endsWith("static/backend/index.html") && !uri.contains("register") && !uri.contains("logout") && !uri.contains("upload")
        		&& !uri.contains("pur")){
            User user = (User) req.getSession().getAttribute("user");
            if(user == null){
                res.sendRedirect("login.html");
            }
        }
        chain.doFilter(req, res);
    }
}
